package com.southwind.controller;

import com.southwind.entity.Menu;
import com.southwind.entity.Order;
import com.southwind.entity.OrderVO;
import com.southwind.entity.User;
import com.southwind.feign.OrderFeign;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;

/**
 * @program: aspringclouddemo
 * @description:
 * @author: xzf
 * @create: 2021-12-10 15:38
 **/
@Controller
@RequestMapping("/order")
public class OrderClientHandler {
    @Resource
    OrderFeign orderFeign;

    @GetMapping("/save/{mid}")
    public String save(@PathVariable("mid") Integer mid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Order order = new Order();
        order.setUser(user);
        Menu menu = new Menu();
        menu.setId(mid);
        order.setMenu(menu);
        orderFeign.save(order);
        return "order";
    }

    //    返回数据
    @GetMapping("/findAllByUid")
    @ResponseBody
    public OrderVO findAllByUid(HttpSession session, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        User user = (User) session.getAttribute("user");
        Integer index = (page - 1) * limit;
        System.out.println(page + " " + index + " " + user);
        return orderFeign.findByUid(index, limit, user.getId());
    }

    //    查询全部订单的数据
    @GetMapping("/findAllOrder")
    @ResponseBody
    OrderVO findAllOrder(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        Integer index = (page - 1) * limit;
        return orderFeign.findAllOrder(index, limit);
    }

    //    修改订单的信息
    @GetMapping("/updateState/{oid}")
    String updateState(@PathVariable("oid") Integer oid) {
        orderFeign.updateState(oid);
        return "order_handler";
    }
}
